package com.base.essearch.es.controller;

import com.base.essearch.es.bean.MovieEntity;
import com.base.essearch.es.bean.PageEntity;
import com.base.essearch.es.service.IMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基于ElasticSearch操作的电影的控制器,底层使用统一代码
 *
 * @date 2019-08-17
 */
@Controller
@RequestMapping("movie")
public class MovieController {

    @Autowired
    private IMovieService movieService;

    /**
     * 保存一条电影的信息
     *
     * @return
     * @date 2019-08-17
     */
    @RequestMapping("/saveMovie")
    @ResponseBody
    public Map<String, Object> saveMovie() {
        Map<String, Object> rMap = new HashMap<String, Object>();
        movieService.saveMovie();
        rMap.put("success", true);
        return rMap;
    }

    /**
     * 保存多条电影的信息
     *
     * @return
     * @date 2019-08-17
     */
    @RequestMapping("/saveBatchMovie")
    @ResponseBody
    public Map<String, Object> saveBatchMovie() {
        Map<String, Object> rMap = new HashMap<String, Object>();
        movieService.saveBatchMovie(new ArrayList<>());
        rMap.put("success", true);
        return rMap;
    }

    /**
     * 查询所有电影
     *
     * @return
     * @date 2019-08-17
     */
    @RequestMapping("/getAllMovie")
    @ResponseBody
    public Map<String, Object> getAllMovie() {
        Map<String, Object> rMap = new HashMap<String, Object>();
        List<MovieEntity> list = movieService.getAllMovie();
        rMap.put("list", list);
        rMap.put("success", true);
        return rMap;
    }

    /**
     * 分页查询所有电影
     *
     * @return
     * @date 2019-08-18
     */
    @RequestMapping("/getAllMovieForPage")
    @ResponseBody
    public Map<String, Object> getAllMovieForPage(String menu, String search, String selectType,
                                                  @RequestParam(defaultValue = "1") int page,
                                                  @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> rMap = new HashMap<String, Object>();

        PageEntity pageObj = movieService.getAllMovieForPage(menu, search, selectType, page, pageSize, true);
        rMap.put("page", pageObj);
        rMap.put("success", true);
        return rMap;
    }


    /**
     * 爬虫下载所有电影
     *
     * @return
     * @date 2019-08-18
     */
    @RequestMapping("/startDownloadMovie")
    @ResponseBody
    public Map<String, Object> startDownloadMovie() {
        Map<String, Object> rMap = new HashMap<String, Object>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                movieService.startDownloadMovie();
            }
        }).start();
        rMap.put("success", true);
        return rMap;
    }

    /**
     * 删除所有数据
     *
     * @return
     * @date 2019-08-18
     */
    @RequestMapping("/deleteAllMovie")
    @ResponseBody
    public Map<String, Object> deleteAllMovie() {
        Map<String, Object> rMap = new HashMap<String, Object>();
        movieService.deleteAllMovie();
        rMap.put("success", true);
        return rMap;
    }

    /**
     * 删除指定ID的数据
     *
     * @return
     * @date 2019-09-07
     */
    @RequestMapping("/deleteMovieById")
    @ResponseBody
    public Map<String, Object> deleteMovieById(String movieId) {
        Map<String, Object> rMap = new HashMap<String, Object>();
        movieService.deleteMovieById(movieId);
        rMap.put("success", true);
        return rMap;
    }


}
